package com.kaisademo.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaisademo.zhxy.mapper.AdminMapper;
import com.kaisademo.zhxy.pojo.Admin;
import com.kaisademo.zhxy.pojo.LoginForm;
import com.kaisademo.zhxy.service.AdminService;
import com.kaisademo.zhxy.util.MD5;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Override
    public Admin login(LoginForm loginForm) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getName,loginForm.getUsername());
        queryWrapper.eq(Admin::getPassword, MD5.encrypt(loginForm.getPassword()));
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public Admin getAdminById(Long userId) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getId,userId);
        Admin admin = baseMapper.selectOne(lambdaQueryWrapper);
        return admin;
    }

    @Override
    public IPage<Admin> getAllAdmin(Page<Admin> adminPage, Admin admin) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(admin.getName() != null){
            lambdaQueryWrapper.like(Admin::getName,admin.getName());
        }
        lambdaQueryWrapper.orderByDesc(Admin::getId);
        Page<Admin> selectPage = baseMapper.selectPage(adminPage, lambdaQueryWrapper);
        return selectPage;
    }
}
